package com.zsrt.oa.integral.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zsrt.oa.integral.dao.IntegralDao;
import com.zsrt.oa.integral.entity.IntegralEntity;
import com.zsrt.utils.DBUtils;

/**
 * 积分模块的实现类
 * @author 罗智壕-13424431416
 *
 * 2017年6月12日
 */
public class IntegralDaoImpl implements IntegralDao {
	
	/**
	 * 查询当前用户获得的积分
	 * @param userPhone
	 * @return
	 * @throws SQLException
	 */
	@Override
	public List<IntegralEntity> integralQuery(String userPhone) throws SQLException {
		List<IntegralEntity> list = new ArrayList<IntegralEntity>();
		Connection con = DBUtils.getMessage();
		String sql = "select "
				+ " order_id as orderId,"
				+ " ig_id as igId,"
				+ " USER_ID as userId,"
				+ " IG_AMOUNT as igAmount "
				+ " from tkt_integal_t where user_id = ?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setString(1, userPhone);
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			IntegralEntity entity = new IntegralEntity();
			entity.setIgId(rs.getInt("igId"));
			entity.setOrderId(rs.getInt("orderId"));
			entity.setUserId(rs.getString("userId"));
			entity.setIgAmount(rs.getDouble("igAmount"));
			list.add(entity);
		}
		
		DBUtils.closeResources(con, ps, rs);
		return list;
	}
	
	/**
	 * 增加积分记录
	 * @param entity
	 * @return
	 * @throws SQLException
	 */
	@Override
	public int integralAdd(IntegralEntity entity) throws SQLException {
		int counter = 0;
		Connection con = DBUtils.getMessage();
		String sql = "insert into tkt_integal_t (user_id,IG_AMOUNT,ORDER_ID) values(?,?,?)";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setString(1, entity.getUserId());
		ps.setDouble(2, entity.getIgAmount());
		ps.setInt(3, entity.getOrderId());
		counter = ps.executeUpdate();
		DBUtils.closeResources(con, ps);
		return counter;
	}

}
